package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.RaceFeedback;
import com.java.entity.event.RaceCommentEvent;
import com.java.entity.event.RaceFeedbackEvent;
import com.java.mapper.RaceAccountMapper;
import com.java.mapper.RaceFeedbackMapper;
import com.java.service.RaceFeedbackService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class RaceFeedbackServiceImpl implements RaceFeedbackService {

	@Autowired
	private RaceFeedbackMapper feedbackMapper;
	@Autowired
	private RaceAccountMapper accountMapper;
	
	@Override
	public BaseRespone createFeedback(String userId, String content) {
		RaceFeedback info = new RaceFeedback().setFeedbackId(MyTools.getUUID())
				.setFeedbackUserId(userId).setFeedbackContent(content).setFeedbackTime(new Date());
		int row = feedbackMapper.insertSelective(info);
		return BaseRespone.result(row, "已留言，请等待回复");
	}

	@Override
	public BaseRespone replyFeedback(String feedbackId, String reply) {
		RaceFeedback info = new RaceFeedback().setFeedbackId(feedbackId)
				.setFeedbackReply(reply).setFeedbackReplyTime(new Date());
		int row = feedbackMapper.updateSelective(info);
		return BaseRespone.result(row, "已回复");
	}

	@Override
	public BaseRespone delFeedback(String feedbackId) {
		int row = feedbackMapper.deleteByKey(feedbackId);
		return BaseRespone.result(row, "已删除");
	}

	@Override
	public TableRespone getFeedbacks(String userId, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (null != userId) {
			params.put("feedbackUserId", userId);
		}
		List<RaceFeedbackEvent> list = feedbackMapper.selectAll(params, page, size);
		for (RaceFeedbackEvent feedback : list) {
			feedback.setFeedbackUser(accountMapper.selectByKey(feedback.getFeedbackUserId()));
		}
		long count = feedbackMapper.selectAllCount(params);
		return new TableRespone(list, count);
	}

}
